package com.foreveross.chameleon.router;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;

public class RoutingParserHelper {
	public int r;
	public Context context;
	
	public RoutingParserHelper(Context context,int r) {
		this.r = r;
		this.context = context;
	}
	public RoutingParserHelper() {
	}
	
	public static ArrayList<RoutingModel> routings = new ArrayList<RoutingModel>();
	
	public ArrayList<RoutingModel> getRoutings() {
		return routings;
	}


	public void setRoutings(ArrayList<RoutingModel> routings) {
		this.routings = routings;
	}


	public  void  addList(RoutingModel model) {
		routings.add(model);
	}
		
	
	public void readConfig() {

		InputStream is = context.getResources().openRawResource(
				r);
		String content = null;
		try {
			byte buffer[] = new byte[is.available()];
			is.read(buffer);
			content = new String(buffer);
			System.out.println("content====������������ "+content);
		} catch (IOException e) {
		} finally {
			if (is != null) {
				try {
					is.close();
				} catch (IOException e) {
				}
			}

		}
		
		try {
			jsonTool(content);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@SuppressLint("NewApi")
	public ArrayList<RoutingModel> jsonTool (String json) throws JSONException {
		JSONObject jsonObject =  new JSONObject(json);
		HashMap<String, MappingModel>   list = new HashMap<String, MappingModel>();
		JSONArray jsonArray = new JSONArray(jsonObject.getString("mappings"));
		RoutingModel r = new RoutingModel();
		for(int i =0;i<jsonArray.length();i++) {
			MappingModel m = new MappingModel();
			JSONObject jsonObjects =  new JSONObject(jsonArray.get(i).toString());
			String key = jsonObjects.keys().next().toString();
			
			Pattern p = Pattern.compile(".+?(\\{.+?\\})");
			Matcher matcher = p.matcher(key);
			String value = "";
			if (matcher.find()) {
				value = key.split("\\{")[0];
				value = value.substring(0,value.length()-1);
				m.setLinkURL(value);
			}else {
			m.setLinkURL(key);
			}
			m.setPageIdentifier(jsonObjects.getString(key));
			list.put(jsonObjects.getString(key), m);
			r.setMappings(list);
		}
		getRoutings().add(r);
		
		return routings;
		
	}
	
	//����url link ������ҳ��
	float maxIndex = 0;
	MappingModel mappingModel;
	public MappingModel redirectToPage(String url) {
		String urls[] =url.split("/");
		for(int i =0;i<routings.size();i++) {
			Iterator iter = routings.get(i).getMappings().entrySet().iterator();
			while (iter.hasNext()) {
				Entry entry = (Entry) iter.next();
				MappingModel val = (MappingModel) entry.getValue();
				float similarityRatio = getSimilarityRatio(url, val.getLinkURL());
				if(maxIndex<similarityRatio) {
					if((urls[1]).equals((val.getLinkURL().split("/")[1])) ) {
						maxIndex = similarityRatio;
						mappingModel  = val;
					}else {
						maxIndex = 0;
						mappingModel = null;
					}
				}
			}
		}
		return mappingModel;
	}
	
	
	
	private int compare(String str, String target) {
		int d[][]; // ����
		int n = str.length();
		int m = target.length();
		int i; // ����str��
		int j; // ����target��
		char ch1; // str��
		char ch2; // target��
		int temp; // ��¼��ͬ�ַ�,��ĳ������λ��ֵ������,����0����1
		if (n == 0) {
			return m;
		}
		if (m == 0) {
			return n;
		}
		d = new int[n + 1][m + 1];
		for (i = 0; i <= n; i++) { // ��ʼ����һ��
			d[i][0] = i;
		}

		for (j = 0; j <= m; j++) { // ��ʼ����һ��
			d[0][j] = j;
		}

		for (i = 1; i <= n; i++) { // ����str
			ch1 = str.charAt(i - 1);
			// ȥƥ��target
			for (j = 1; j <= m; j++) {
				ch2 = target.charAt(j - 1);
				if (ch1 == ch2) {
					temp = 0;
				} else {
					temp = 1;
				}

				// ���+1,�ϱ�+1, ���Ͻ�+tempȡ��С
				d[i][j] = min(d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1]
						+ temp);
			}
		}
		return d[n][m];
	}

	private int min(int one, int two, int three) {
		return (one = one < two ? one : two) < three ? one : three;
	}

	public float getSimilarityRatio(String str, String target) {
		return 1 - (float) compare(str, target)
				/ Math.max(str.length(), target.length());
	}
}
